﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Forms;
using BulkRenamer.Common.Models;
using CommonUtilities.Util;
#endregion

namespace BulkRenamer.App.Views
{
    public partial class frmBatchSelector : Form
    {
        #region Properties
        /// <summary>
        /// Log details
        /// </summary>
        public ObservableCollection<LogDetails> AllLogDetails { get; set; }

        /// <summary>
        /// Selected BatchStamp
        /// </summary>
        public string SelectedBatchStamp { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes form and loads log details
        /// </summary>
        /// <param name="allLogDetails">Log details</param>
        public frmBatchSelector(ObservableCollection<LogDetails> allLogDetails)
        {
            InitializeComponent();

            AllLogDetails = new ObservableCollection<LogDetails>(allLogDetails.OrderByDescending(logdet => logdet.BatchStamp));
            tltipBatchSelector.SetToolTip(dgBatchSelector,
                "Dbl. Click to see preview..." + Utilities.cNewLine +
                "Clcik batch and press '" + btnDone.Text + "' to select batch; OR");

            dgBatchSelector.AutoGenerateColumns = false;
            dgBatchSelector.DataSource = new ObservableCollection<LogDetails>(
                AllLogDetails.Select(logdet => new { Stamp = logdet.BatchStamp, Desc = logdet.Description }).Distinct()
                    .Select(nw => new LogDetails { BatchStamp = nw.Stamp, Description = nw.Desc }));
        }
        #endregion

        #region Events
        private void dgBatchSelector_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                string batchStamp = dgBatchSelector.Rows[e.RowIndex].Cells[colBatchStamp.Name].Value.ToString();

                if (Form.ModifierKeys == Keys.Control)
                { Clipboard.SetText(batchStamp); return; }

                ObservableCollection<FileDetails> allFileDetails = new ObservableCollection<FileDetails>(
                    AllLogDetails.Where(logdet => logdet.BatchStamp == batchStamp).Cast<FileDetails>());

                frmGridPreview frmFilePreview = new frmGridPreview(allFileDetails, true);
                frmFilePreview.ShowDialog(this);

                frmFilePreview.Dispose();
                frmFilePreview = null;
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            if (dgBatchSelector.SelectedRows.Count == 1)
                SelectedBatchStamp = dgBatchSelector.SelectedRows[0].Cells[colBatchStamp.Name].Value.ToString();

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            SelectedBatchStamp = "";
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
        #endregion

        private void pnlBorder_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
